/**
 * @author Troy Gardner
 */
import com.troyworks.datastructures.Array2;
import com.troyworks.datastructures.WindowIterator;
import com.troyworks.framework.ui.BaseComponent;
import com.troyworks.hsmf.AEvent;
import com.troyworks.datastructures.RandomizedPlayList;
import com.troyworks.geom.d1.CompoundLine1D;
import com.troyworks.geom.d1.Line1D;
import com.troyworks.ui.menus.Table3DRotaryCarouselMenuItem;
import com.troyworks.mediaplayer.ui.FramedMediaPlayer;
import com.troyworks.framework.ui.layout.Bounds2;

class com.troyworks.ui.menus.CarouselMenu extends BaseComponent {
	public var playlistvisiblewindow_mc : MovieClip;
	public var clipPadding : Number = 3;
	var clippadding : Number = 5;
	//playlist UI clips
	var menuUIItems : Array2;
	var menuDataItems : Array2;
	var heirarchyDepth : Number = 0;
	var data_xml : XML;
	public static var SELECTED_SLIDE_EVT : AEvent = AEvent.getNext("SELECTED_SLIDE");

	private var rate : Number;

	private var _oy : Number;

	private var playList : Array2;

	private var playlistViewModel : CompoundLine1D;

	private var centerX : Number;

	private var lastMousePos : String;

	private var _xadj : Number = 0;
	private var speed : Number = 0;
	public var curSelectedClip : Object;
	//////////////////CONSTRUCTOR///////////////////////////////
	public function CarouselMenu() {
		super(null, "CarouselMenu", false);
		trace(" CarouselMenu");
		menuUIItems =  new Array2();
		menuDataItems =  new Array2();
		//////////////Activate the statemachine ///////////
		init();
	}
	public function getPlaylist() : RandomizedPlayList{
		var pl : RandomizedPlayList = new RandomizedPlayList();
		pl.appendArray(["A"," B","C","D", "E"]);
		trace("playlistA " + pl.toString(true));
		return pl;		
	}
	public function parsePlayList(playList : RandomizedPlayList) : Void{
	trace(" playList parse:" + playList.toString(true));
		playlistViewModel = new CompoundLine1D("PlayList", 1, 0, 0,null);
		var c : Number = 0;
		var curX : Number = 0;
		var itemWidth : Number = 90;
		while(playList.hasNext()){
			var page : Object = playList.getNext();
			//trace("Adding Cell " + page);
			var line : Line1D = new Line1D("PlayListItem", 1, playlistViewModel.B,itemWidth,null);
			line.data = page;
			playlistViewModel.addChild(line);
			if(c == 200){
				trace(" parsePlayList breaking loop 200 items!");
				break;
			}			
		}
	//	trace("HIGHLIGHT parsed PlayListViewModel"+ playlistViewModel);
	}
	public function setXY(x : Number, y : Number) : Void{
		_x = x;
		_y = y;
	}
	public function setWidthHeight(aW : Number, h : Number) : Void{
		trace("HIGHLIGHTP KTClientMediaPlayer.setWidthHeight w " + aW + " h " + h);
		playlistvisiblewindow_mc._width = aW;
		playlistvisiblewindow_mc._height = h;
	}
	public function setX(aX : Number) : Void {
		setXY(aX, _y);
	}
	public function setY(aY : Number) : Void {
		setXY(_x, aY);
	}
	public function setWidth(aWidth : Number) : Void {
		trace("MEDIA PLAYER.setWidth " + aWidth);
		setWidthHeight(aWidth, null);
	}
	public function setHeight(aHeight : Number) : Void {
		trace("MEDIA PLAYER.setHeight " + aHeight);
		setWidthHeight(null, aHeight);
	}
	///////////////////////////// EVENTS ///////////////////////////////////////////
	public function hide() : Void{
		Q_TRAN(s2_hiddenCarousel);
	}
	public function show() : Void{
		Q_TRAN(s2_animatingCarousel);
	}
	public function onLoad() : Void{
		super.onLoad();
		trace("onLoad");
		centerX = this._width/2;
		_owidth = this._width;
		//playlistvisiblewindow_mc._visible =true;
		Q_dispatch(ASSETS_LOADED_EVT);
//		
	}
		/********************************
	* This configures the rotation
	* rate based on the mouses position
	* there are 3 basic zones,
	* left side, right side and center.
	* center is a slow/zero zone, setting to zero when crossing center.
	*/
	function onMouseMove() : Void {
		var b2:Bounds2 = new Bounds2(playlistvisiblewindow_mc.getBounds());
		//trace("_xmouse " + _xmouse + " " +b2);
		if(b2.contains(playlistvisiblewindow_mc._xmouse,playlistvisiblewindow_mc._ymouse)){
			if(myCurState == s2_pauseCarousel){
				Q_TRAN(s2_animatingCarousel);
			}
			
			if (playlistvisiblewindow_mc._xmouse<(50)) {
					lastMousePos = "LEFT_SIDE";
				
					rate = -5;//(this._xmouse-(centerX-100 + _xadj));//_owidth;
					trace("GO -- " + rate);
			} else if ((b2.xMax-50) <playlistvisiblewindow_mc._xmouse) {
					lastMousePos = "RIGHT_SIDE";
					rate = 5;//(this._xmouse-(centerX+100 + _xadj));///_owidth;
					trace("--GO " + rate);
			} else {
				trace("SPEED C "+speed);
				if (rate != 0) {
					if (lastMousePos == "LEFT_SIDE" && _xmouse>centerX) {
						rate = 0;
					} else if (lastMousePos == "RIGHT_SIDE" && _xmouse<centerX) {
						rate = 0;
					}
				}
			}
			var maxSpeed:Number = 3;
			//rate = Math.max((-1* maxSpeed),Math.min(rate,maxSpeed));
			trace(" throttled rate "  + rate);
		}else{
			rate = 0;
			if(myCurState == s2_animatingCarousel){
				Q_TRAN(s2_pauseCarousel);
			}
		}
	};
	//////////////////////// LEVEL 0 STATES////////////////////////////
/*.................................................................*/
	function s1_creatingView(e : AEvent) : Function
	{
		this.onFunctionEnter ("s1_creatingView-", e, []);
		switch (e)
		{
			case ENTRY_EVT :
			{
				trace("HIGHLIGHTL s1_creatingView ");
				var pl : RandomizedPlayList = getPlaylist();
				parsePlayList(pl);
				var cx : Number = 0;
				var ini1 : Object = new Object();
				ini1._x = cx;			
				var USEBOOKENDS:Boolean = true;
				if(USEBOOKENDS){
					//////////// PLACE START BOOKEND /////////////////
					var menuStart : MovieClip = attachMovie("PlaylistStart", "photoStart", getNextHighestDepth(),ini1);
					menuStart.width =40;
					menuUIItems.push(menuStart);

					cx+= 40;
				}
				////////// PLACE BOOKS /////////////////
				for (var i : Number = 0; i < playlistViewModel.children.length; i++) {
					var l : Line1D = playlistViewModel.children[i];
					trace("line " + l);
					var ini : Object = new Object();
					ini._x = l.A + cx;
					cx += l.B.position;
					var menuItem : FramedMediaPlayer = FramedMediaPlayer(attachMovie("PlaylistPhotoButton", "photo"+i, getNextHighestDepth(),ini));
					trace("creating View " + util.Trace.me(l.data, "DATA", true));
					menuItem.contentURL = l.data.smallIcon;//"users/icon1.PNG";
					menuItem.label = l.data.shortName;
					menuItem.data = l.data;
					Object(menuItem).menu = this;
					menuItem.addEventListener("CLICK",this, this.createCallback(SELECTED_SLIDE_EVT));
					menuItem.addEventListener("CLICK", this, this.onChildClicked);
					menuItem.mode = Table3DRotaryCarouselMenuItem.LINEAR_CAROUSEL_MODE;
					decorateClip(menuItem);
					menuUIItems.push(menuItem);
				}
				if(USEBOOKENDS){
					////////// PLACE END BOOKEND /////////////////
					var ini2 : Object = new Object();
					ini2._x = cx;

					var menuEnd : MovieClip = attachMovie("PlaylistEnd", "photoEnd", getNextHighestDepth(),ini2);
					menuEnd.width = 40;
					decorateClip(menuEnd);
					menuUIItems.push(menuEnd);
				}
				return null;
			}
			case EXIT_EVT :
			{
				return null;
			}
			case INIT_EVT :
			{
				Q_TRAN(s2_animatingCarousel);
				return null;
			}
		}
		return this.s0_viewAssetsLoaded;
	}

	////////////////////////////LEVEL 2 States ///////////////////////
	/*.................................................................*/
	function s2_animatingCarousel(e : AEvent) : Function
	{
		this.onFunctionEnter ("s2_animatingCarousel-", e, []);
		switch (e)
		{
			case ENTRY_EVT :
			{
				startPulse(1000/24);
				return null;
			}
			case EXIT_EVT :
			{
				stopPulse();
				return null;
			}
			case INIT_EVT :
			{
				return null;
			}
			case SELECTED_SLIDE_EVT:
			{
				trace("000000000000000000000000000000000000000000000000000000");
				trace("000000000000000000000000000000000000000000000000000000");
				trace("00000000000000000000SELECTED_SLIDE_EVT0000000000000000000000000000000000");
				trace("000000000000000000000SELECTED_SLIDE_EVT000000000000000000000000000000000");
				trace("000000000000000000000000000" + util.Trace.me( _parent.main_window)+ "000000000000000000000000000");
				trace("000000000000000000000000000000000000000000000000000000");
				_parent.main_window.mxP.setContentTo(curSelectedClip.data.contentURL);
				_parent.main_window.setEnabled(false);
				dispatchEvent({type:SELECTED_SLIDE_EVT, data:curSelectedClip.data});
				break;
			}
			case PULSE_EVT:
			{
			//	var xpos : Number = Math.min(Math.max(0, (this._xmouse-this.playlistvisiblewindow_mc._x)/this.playlistvisiblewindow_mc._width), 100);
				///trace("***********************************");
			//trace("*************rate " + rate + " **********************");
			//	var acceleration = Math.cos(Math.PI*xpos)*rate*-1;
				var acceleration : Number = rate;
	//			var cx : Number = (playlistvisiblewindow_mc._width < playlistViewModel.length)? Math.round(menuUIItems.getFirstElement()._x-acceleration):0;
				var cx : Number = Math.round(menuUIItems.getFirstElement()._x-acceleration);
			
				//trace(" this "+this+" parent "+this.enterBook+" move? "+(this._y<(this._oy+playlistvisiblewindow_mc.getBounds().yMax+20)));
				/*	if ((this.enterBook == true) && (this._y<(this._oy+playlistvisiblewindow_mc.getBounds().yMax-20))) {
					this._y += 4;
					this._parent.pagelist._y -= 4;
				} else if ((this.enterBook == false) && this._y>this._oy) {
					this._y -= 4;
					this._parent.pagelist._y += 4;
				}*/
				//if (this.enterBook == false) {
				for (var i : Number = 0; i<menuUIItems.length; i++) {
					////////////// MOVE CLIPS (start from left to right)/////////////////////
					var clip : MovieClip = MovieClip(menuUIItems[i]);
					//trace("moving clip " + i + " " + clip + "cx " + cx);
					clip._x = (clip.isToggled)?clip._x:cx;
					if (clip.zoomme == "UP" && clip._yscale<130) {
					//	trace("zoooming up");
						clip._xscale = clip._yscale=(clip._yscale+2);
					} else if (clip.zoomme == "DOWN" && clip._yscale>100) {
					//	trace("zoooming down");
						clip._xscale = clip._yscale=(clip._yscale-2);
					}
					cx = cx+clip.width*(clip._xscale/100)+clippadding;
				}
				for (var i : Number = 0; i<menuUIItems.length; i++) {
					var clip : MovieClip = MovieClip(menuUIItems[i]);
					////////////  RECYCLE CLIPS ////////////////////////////////////
					if (acceleration>=0) {
						var xMax:Number = clip._x+clip.width;
						if (xMax<playlistvisiblewindow_mc._x) {
							trace("******************RECYCLING FROM LEFT***********"+clip);
							var lastClip : MovieClip = MovieClip(menuUIItems.getLastElement());
							//use the orignal widht not the current width
							clip._x = lastClip._x+lastClip.width+clippadding;
				//			clip.label_txt.text = recycles++;
							clip._alpha = 100;
							menuUIItems.shiftTowardsEnd();
						} else if (clip._x<playlistvisiblewindow_mc._x && playlistvisiblewindow_mc._x<xMax) {
							clip._alpha = (1-(playlistvisiblewindow_mc._x-clip._x)/clip.width)*100;
						} else if (clip._x<(playlistvisiblewindow_mc._x+playlistvisiblewindow_mc._width) && (playlistvisiblewindow_mc._x+playlistvisiblewindow_mc._width)<xMax) {
							var a = (((playlistvisiblewindow_mc._x+playlistvisiblewindow_mc._width)-clip._x)/clip.width)*100;
							clip._alpha = a;
							//
						}
					} else if (acceleration<0) {
						var lastClip : MovieClip = MovieClip(menuUIItems.getLastElement());
						var xMax = lastClip._x + lastClip.width;
						if (playlistvisiblewindow_mc._x+playlistvisiblewindow_mc._width<lastClip._x) {
							trace("******************RECYCLING FROM RIGHT***********"+lastClip);
							var firstClip : MovieClip = MovieClip(menuUIItems.getFirstElement());
							//use the orignal widht not the current width
							lastClip._x = firstClip._x-lastClip.width-clippadding;
			//					clip.label_txt.text = recycles++;
							trace("BEFORE "+menuUIItems.join("\r"));
							menuUIItems.shiftTowardsStart();
							trace("AFTER "+menuUIItems.join("\r"));
						} else if (clip._x<playlistvisiblewindow_mc._x && playlistvisiblewindow_mc._x<xMax) {
							var a = (1-(playlistvisiblewindow_mc._x-clip._x)/clip.width)*100;
							//trace("BIRTH" + a);
							clip._alpha = a;
						} else if (clip._x<(playlistvisiblewindow_mc._x+playlistvisiblewindow_mc._width) && (playlistvisiblewindow_mc._x+playlistvisiblewindow_mc._width)<xMax) {
						//	trace("BIRTH");
							var a = (((playlistvisiblewindow_mc._x+playlistvisiblewindow_mc._width)-clip._x)/clip.width)*100;
							clip._alpha = a;
							//
						}
					}
						//}
				}
				return null;
			}
		}
		return this.s1_viewCreated;
	}
	function s2_pauseCarousel(e : AEvent) : Function
	{
		this.onFunctionEnter ("s2_pauseCarousel-", e, []);
		switch (e)
		{
			case ENTRY_EVT :
			{
				return null;
			}
			case EXIT_EVT :
			{
				return null;
			}

		}
		return this.s1_viewCreated;
	}
	function s2_hiddenCarousel(e : AEvent) : Function
	{
		this.onFunctionEnter ("s2_hiddenCarousel-", e, []);
		switch (e)
		{
			case ENTRY_EVT :
			{
				this._visible = false;
				return null;
			}
			case EXIT_EVT :
			{
				this._visible = true;
				return null;
			}

		}
		return this.s1_viewCreated;
	}
/////////////////////////////////////////////////////////////////////////////
//  Model Related Functions
////////////////////////////////////////////////////////////////////////////
	public function setDataProvider(_ary : Array) : Void{
	
	}
	public function onChildClicked() : Void {
		trace("onChildClicked");
	}

	function decorateClip(_mc : MovieClip) : Void {
	//_mc.cacheAsBitmap = true;
		_mc.onRollOut = function() {
			trace("ROLLOVER");
			Object(this).zoomme = "DOWN";
	};
		_mc.onRollOver = function() {
			trace("ROLLOUT");
			Object(this).zoomme = "UP";
		//this._xscale =103;
	};
		_mc.onRelease = function() {
			trace("*** CLICK *******"+MovieClip(this)._parent+" 2 "+MovieClip(this)._parent);
			MovieClip(this)._parent.enterBook = !MovieClip(this)._parent.enterBook;
	};
		_mc.zoomCount = 0;
}

	
}